package com.example.liunanxin.lnx226ordering.model;

import com.example.liunanxin.lnx226ordering.beans.FoodBean;
import com.example.liunanxin.lnx226ordering.iface.FoodListener;
import com.example.liunanxin.lnx226ordering.iface.Foodiface;
import com.example.liunanxin.lnx226ordering.retrofitservice.FoodService;
import com.example.liunanxin.lnx226ordering.url.Url;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FoodModel implements Foodiface {
        private Retrofit retrofit;

        public FoodModel(){
            retrofit=new Retrofit.Builder()
                    .baseUrl(Url.USERURL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }

        @Override
        public void getFoodResult(int shop_id,final FoodListener foodListener) {
            FoodService foodService=retrofit.create(FoodService.class);
            Call<List<FoodBean>> call=foodService.getFoodShop(shop_id);
            call.enqueue(new Callback<List<FoodBean>>() {
                @Override
                public void onResponse(Call<List<FoodBean>> call, Response<List<FoodBean>> response) {

                    if(response.isSuccessful()&& response!=null){
                        foodListener.onResponse(response.body());
                    }
                    else foodListener.onFail("onresponse fail");
                }

                @Override
                public void onFailure(Call<List<FoodBean>> call, Throwable t) {

                    foodListener.onFail(t.toString());
                }
            });
        }

    }

